// import co from 'co'

// const OSSConfig = {
//     uploadHost: sessionStorage.getItem("uploadHost"), //OSS上传地址
//     type: 'scs',
//     ossParams: {
//         region: 'oss-cn-beijing',
//         success_action_status: '200', // 默认200
//         accessKeyId: sessionStorage.getItem("accessKeyId"),
//         accessKeySecret: sessionStorage.getItem("accessKeySecret"),
//         bucket: 'admin',
//     },
// }

// function random_string(len) {
//     len = len || 32
//     var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
//     var maxPos = chars.length
//     var pwd = ''
//     for (let i = 0; i < len; i++) {
//         pwd += chars.charAt(Math.floor(Math.random() * maxPos))
//     }
//     return pwd
// }

// /**
//  *
//  * @param event 上传图片的资源路径
//  * @param type 图片资源类型
//  * @param dir oss要保存的文件夹
//  * @returns {OSS文件路径}
//  */

// function uploadOSS(event, type, dir = 'file/') {
//     return new Promise((resolve, reject) => {
//         var client = new OSS({
//             region: OSSConfig.ossParams.region,
//             accessKeyId: OSSConfig.ossParams.accessKeyId,
//             accessKeySecret: OSSConfig.ossParams.accessKeySecret,
//             bucket: OSSConfig.ossParams.bucket,
//         });
//         var file;
//         if (type == 0) {
//             file = event;
//         } else {
//             file = event.target.files[0];
//         }
//         let randomName = `${dir}${random_string(6)}_${file.name}`;
//         co(function* () {
//             var result = yield client.multipartUpload(randomName, file);
//             console.log(`${OSSConfig.uploadHost}/${result.name}`);
//             resolve(`${OSSConfig.uploadHost}/${result.name}`);
//         }).catch(function (err) {
//             console.log(err);
//             reject(err);
//         });
//     });
// }

// export { uploadOSS }
/**
 * 阿里云oss上传工具
 */
let OSS = require('ali-oss');
// const OSSConfig = {
//     uploadHost: sessionStorage.getItem("uploadHost"), //OSS上传地址
//     type: 'scs',
//     ossParams: {
//         region: 'oss-cn-beijing',
//         success_action_status: '200', // 默认200
//         accessKeyId: sessionStorage.getItem("accessKeyId"),
//         accessKeySecret: sessionStorage.getItem("accessKeySecret"),
//         bucket: 'admin',
//     },
// }
let config = {
    region: 'oss-cn-shenzhen',
    secure: true,
    accessKeyId: "LTAI4FyP3GWaKnHKgDs5bZLW",
    accessKeySecret: "QWhXP174m879WHT1ZKpnxSnQw484OI",
    bucket: 'bsm-sdn'
};

/** 
 * 配置
 */
let init = () => {
    return new OSS(config);
}

/** 
 * 生成uuid
 */
let guid = () => {
    let S4 = () => {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

/** 
 * 修改文件名字
 */
let fileName = (file) => {
    let arr = file.name.split(".");
    var uuid = "oss" + guid();
    if (arr.length > 1) {
        return uuid + '.' + arr[arr.length - 1];
    } else {
        return uuid;
    }
}

/**
 * 上传文件
 */
let ossPut = (file) => {

    return new Promise((resolve, reject) => {
        let objectName = fileName(file);
        console.log("file", objectName)
        console.log("file", file)
        init().put(objectName, file).then(({ res, url }) => {
            if (res && res.status == 200) {
                console.log('阿里云OSS上传文件成功回调', res, url);
                resolve(res, url);
            }
        }).catch((err) => {
            console.log('阿里云OSS上传文件失败回调', err);
            reject(err)
        });
    })
}

/**
 * 下载文件
 */
let ossGet = (name) => {
    return new Promise((resolve, reject) => {
        init().get(name).then(({ res }) => {
            if (res && res.status == 200) {
                console.log('阿里云OSS下载文件成功回调', res);
                resolve(res);
            }
        }).catch((err) => {
            console.log('阿里云OSS下载文件失败回调', err);
            reject(err)
        });
    })
}

export default { ossPut, ossGet }